from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr

import smtplib


def _format_addr(s):
    name, addr = parseaddr(s)
    return formataddr((Header(name, 'utf-8').encode(), addr))


# 输入Email地址和口令:
from_addr = input('qq邮箱地址')
password = input('qq邮箱授权码')
# 输入收件人地址:
to_addr = input('接收人的邮箱')
# 输入SMTP服务器地址:
smtp_server = input('smtp.qq.com')

msg = MIMEText('你是，我是python高手系列', 'plain', 'utf-8')
msg['From'] = _format_addr('python的同学 <%s>' % from_addr)
msg['To'] = _format_addr('管理员 <%s>' % to_addr)
msg['Subject'] = Header('来自SMTP的邮件……', 'utf-8').encode()

server = smtplib.SMTP(smtp_server, 25)
server.set_debuglevel(1)
server.login(from_addr, password)
server.sendmail(from_addr, [to_addr], msg.as_string())
server.quit()

